package cn.itcast.web.controller.cargo;

import cn.hutool.core.bean.BeanUtil;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportProduct;
import cn.itcast.domain.cargo.ExportProductExample;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.util.DownloadUtils;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = "/cargo/export", name = "pdf报表生成")
public class PdfController extends BaseController {

    @RequestMapping("/demo1")
    public void demo1() throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/demo1.jasper");
        // 2.填充数据
        /*
            参数一：模板路径
            参数二：map集合类型数据
            参数三：list集合类型数据（JRDataSource）
         */
        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, new HashMap<>(), new JREmptyDataSource());

        //  3.生成PDF输出到客户端
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }

    @RequestMapping("/demo2")
    public void demo2() throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/demo2.jasper");
        // 2.填充数据
        /*
            参数一：模板路径
            参数二：map集合类型数据
            参数三：list集合类型数据（JRDataSource）
         */
        HashMap<String, Object> map = new HashMap<>();
        map.put("username", "jack");
        map.put("age", 18);
        map.put("address", "深圳");
        map.put("company", "腾讯");

        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, new JREmptyDataSource());

        //  3.生成PDF输出到客户端
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }

    @RequestMapping("/demo3")
    public void demo3() throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/demo3.jasper");
        // 2.填充数据
        /*
            参数一：模板路径
            参数二：map集合类型数据
            参数三：list集合类型数据（JRDataSource）
         */

        List<User> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setUserName("用户" + i);
            user.setEmail(i + "@export.com");
            user.setDeptName("部门" + i);
            user.setCompanyName("传智播客");
            list.add(user);
        }
        JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(list);
        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, new HashMap<>(), jrDataSource);

        //  3.生成PDF输出到客户端
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());
    }

    @Reference
    private ExportService exportService;

    @Reference
    private ExportProductService exportProductService;

    @RequestMapping("/exportPdf")
    public void exportPdf(String id) throws Exception {
        // 1.加载模板
        String realPath = request.getSession().getServletContext().getRealPath("/jasper/export.jasper");
        // 2.填充数据
        /*
            参数一：模板路径
            参数二：map集合类型数据
            参数三：list集合类型数据（JRDataSource）
         */

        // 2.1查询报运单对象转为map集合
        Export export = exportService.findById(id);
        // hutool工具包，可以将对象转为map集合，key=对象属性名，value=对象属性值
        Map<String, Object> map = BeanUtil.beanToMap(export);

        // 2.2查询报运单商品列表转为JRDataSource
        ExportProductExample exportProductExample = new ExportProductExample();
        exportProductExample.createCriteria().andExportIdEqualTo(id);
        List<ExportProduct> list = exportProductService.findAll(exportProductExample);
        JRBeanCollectionDataSource jrDataSource = new JRBeanCollectionDataSource(list);

        JasperPrint jasperPrint = JasperFillManager.fillReport(realPath, map, jrDataSource);

        //  3.生成PDF输出到客户端
        JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream());

        // 4.pdf文件下载
       /* ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); // 创建字节数组输出流
        JasperExportManager.exportReportToPdfStream(jasperPrint, byteArrayOutputStream); // 将pdf输出到字节数组输出流中
        DownloadUtils.download(byteArrayOutputStream, response, "报运单.pdf");// 完成文件下载*/
    }
}
